package cn.uai.activiti8.approval.cmd;

import org.activiti.bpmn.model.EndEvent;
import org.activiti.bpmn.model.Process;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.util.ProcessDefinitionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @desc 驳回到 申请节点
 * @author uai
 * @since 2025/7/18 23:50
 * @version 1.0
 */
public class Reject2ApplyNodeCmd extends AbstractRejectCmd<Void> {

    private static final long serialVersionUID = -414632895409846164L;

    private static final Logger LOGGER = LoggerFactory.getLogger(Reject2ApplyNodeCmd.class);

    public Reject2ApplyNodeCmd(String taskId) {
        super(taskId);
    }

    @Override
    protected Void execute(CommandContext commandContext, TaskEntity task) {
        ExecutionEntity execution = task.getExecution();
        Process process = ProcessDefinitionUtil.getProcess(task.getProcessDefinitionId());
        EndEvent endEvent = findEndEvent(process, task.getProcessDefinitionId());
        execution.setVariable("isPass", false);
        execution.setVariable("actionType", 3);
        LOGGER.info("直接驳回到申请节点");
        execution.setCurrentFlowElement(endEvent);
        commandContext.getAgenda().planContinueProcessOperation(execution);

        // 删除 act_ru_task 中的数据
        task.setExecutionId(null);
        commandContext.getTaskEntityManager().deleteTask(task.getId(), "驳回至申请节点", false);
        // 删除任务还可以用下面这个
        // commandContext.getTaskEntityManager().deleteTask(taskEntity, null, false, false);
        return null;
    }
}
